package org.gwtde.client.controllers;

import java.util.ArrayList;
import java.util.List;

import net.mygwt.ui.client.mvc.AppEvent;
import net.mygwt.ui.client.mvc.Controller;
import net.mygwt.ui.client.mvc.View;
import net.mygwt.ui.client.widget.Shell;

import org.gwtde.client.DesktopEvents;
import org.gwtde.client.views.ShellView;

import com.google.gwt.user.client.Window;

public class ShellController extends Controller {
	
	private List mViews = new ArrayList();
	
	public ShellController() {
		registerEventType(DesktopEvents.ShellOpenURL);
		registerEventType(DesktopEvents.ShellActivated);
		registerEventType(DesktopEvents.ShellDeActivated);
		registerEventType(DesktopEvents.ShellClosed);
		registerEventType(DesktopEvents.DockPinned);
		registerEventType(DesktopEvents.DockUnPinned);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.mygwt.ui.client.mvc.Controller#handleEvent(net.mygwt.ui.client.mvc.AppEvent)
	 */
	public void handleEvent(AppEvent pEvent) {
		switch (pEvent.type) {
		case DesktopEvents.ShellOpenURL:
			View view = new ShellView(this);
			mViews.add(view);
			forwardToView(view, pEvent);
			break;
		case DesktopEvents.DockPinned:
		case DesktopEvents.DockUnPinned:
			for (int i = 0, n = mViews.size(); i < n; i++) {
				forwardToView((View)mViews.get(i), pEvent);
			}
			break;
		case DesktopEvents.ShellClosed:
			mViews.remove((View)((Shell)pEvent.data).getData());
			break;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.mygwt.ui.client.mvc.Controller#initialize()
	 */
	public void initialize() {
		
	}

}
